<?php
/**
 * Created by PhpStorm.
 * User: shaowei
 * Date: 2018/12/21
 * Time: 上午11:44
 */

namespace App\Http\Services;


use App\Http\Model\KovStatic;

use Illuminate\Support\Facades\DB;

class KovServices extends CommonServices
{
    private $kovModel;//模型
    protected $userTable = 'user'; //用户表
    protected $frOrderTable = 'finance_recharge_order'; //充值表
    protected $diamondStreamTable = 'finance_diamond_stream'; //流水表
    protected $userLoginLogTable = 'user_login_log'; //用户登录记录
    protected $table = "kov_statistics"; //kov统计表

    public function __construct()
    {
        $this->kovModel = new KovStatic();
    }

    /**
     * kov数据处理入库
     */
    public function checkAndInsert($userids=[])
    {
        $inserts = [];
        $updatas = [];
        foreach($userids as $uuid) {
            //获取用户信息
            $userInfo = DB::table($this->userTable)
                ->where('uuid', $uuid)
                ->select('uuid', 'user_id', 'create_timestamp')
                ->first();
            $userInfo = $this->objToArray($userInfo);
            $userId = $userInfo['user_id'];
            //注册时间
            $register = $userInfo['create_timestamp'];
            //注册当日
            $registerDaySt = strtotime(date('Y-m-d 00:00:00', $userInfo['create_timestamp']));
            $registerDayEd = strtotime(date('Y-m-d 23:59:59', $userInfo['create_timestamp']));
            //注册三日后
            $registerDayEd3 = $registerDayEd + 86400 * 3;
            //注册七日后
            $registerDayEd7 = $registerDayEd + 86400 * 7;

            //总充值金额钻
            $rechargeAll = DB::table($this->frOrderTable)
                ->where('user_id', $userId)
                ->where('status', 1)
                ->selectRaw('user_id, sum(diamond_amount) as num')
                ->first();
            $rechargeAll = $this->objToArray($rechargeAll);

            //总消费钻石
            $diamondAll = DB::table($this->diamondStreamTable)
                ->where('user_id', $userId)
                ->where('diamond_value', '<', 0)
                ->selectRaw('user_id, sum(diamond_value) as num')
                ->first();
            $diamondAll = $this->objToArray($diamondAll);

            //注册当日充值钻石
            $recharge1 = DB::table($this->frOrderTable)
                ->where('user_id', $userId)
                ->where('status', 1)
                ->where('pay_timestamp', '>=', $registerDaySt)
                ->where('pay_timestamp', '<=', $registerDayEd)
                ->selectRaw('user_id, sum(diamond_amount) as num')
                ->first();
            $recharge1 = $this->objToArray($recharge1);

            //注册当日消费钻石
            $diamond1 = DB::table($this->diamondStreamTable)
                ->where('user_id', $userId)
                ->where('diamond_value', '<', 0)
                ->where('change_timestamp', '>=', $registerDaySt)
                ->where('change_timestamp', '<=', $registerDayEd)
                ->selectRaw('user_id, sum(diamond_value) as num')
                ->first();
            $diamond1 = $this->objToArray($diamond1);

            //注册后3日登陆次数
            $login3 = DB::table($this->userLoginLogTable)
                ->where('user_id', $userId)
                ->where('time', '>=', $registerDaySt)
                ->where('time', '<=', $registerDayEd3)
                ->selectRaw('user_id, count(1) as num')
                ->first();
            $login3 = $this->objToArray($login3);

            //3日充值钻石
            $recharge3 = DB::table($this->frOrderTable)
                ->where('user_id', $userId)
                ->where('status', 1)
                ->where('pay_timestamp', '>=', $registerDaySt)
                ->where('pay_timestamp', '<=', $registerDayEd3)
                ->selectRaw('user_id, sum(diamond_amount) as num')
                ->first();
            $recharge3 = $this->objToArray($recharge3);

            //3日消费钻石
            $diamond3 = DB::table($this->diamondStreamTable)
                ->where('user_id', $userId)
                ->where('diamond_value', '<', 0)
                ->where('change_timestamp', '>=', $registerDaySt)
                ->where('change_timestamp', '<=', $registerDayEd3)
                ->selectRaw('user_id, sum(diamond_value) as num')
                ->first();
            $diamond3 = $this->objToArray($diamond3);

            //7日登陆次数
            $login7 = DB::table($this->userLoginLogTable)
                ->where('user_id', $userId)
                ->where('time', '>=', $registerDaySt)
                ->where('time', '<=', $registerDayEd7)
                ->selectRaw('user_id, count(1) as num')
                ->first();
            $login7 = $this->objToArray($login7);

            //7日充值钻石
            $recharge7 = DB::table($this->frOrderTable)
                ->where('user_id', $userId)
                ->where('status', 1)
                ->where('pay_timestamp', '>=', $registerDaySt)
                ->where('pay_timestamp', '<=', $registerDayEd7)
                ->selectRaw('user_id, sum(diamond_amount) as num')
                ->first();
            $recharge7 = $this->objToArray($recharge7);

            //7日消费钻石
            $diamond7 = DB::table($this->diamondStreamTable)
                ->where('user_id', $userId)
                ->where('diamond_value', '<', 0)
                ->where('change_timestamp', '>=', $registerDaySt)
                ->where('change_timestamp', '<=', $registerDayEd7)
                ->selectRaw('user_id, sum(diamond_value) as num')
                ->first();
            $diamond7 = $this->objToArray($diamond7);
            //检查如果存在uuid则重新更新数据
            $status = DB::table($this->table)->where('uuid',$uuid)->exists();
            if ($status)
            {
                $updata = [
                    'uuid' => $uuid,
                    'userId' => $userId,
                    'registerDay' => date("Y-m-d H:i:s", $register),
                    'rechargeAll' => empty($rechargeAll['num']) ? 0 : $rechargeAll['num'],
                    'diamondAll' => empty($diamondAll['num']) ? 0 : abs($diamondAll['num']),
                    'recharge1' => empty($recharge1['num']) ? 0 : $recharge1['num'],
                    'diamond1' => empty($diamond1['num']) ? 0 : abs($diamond1['num']),
                    'login3' => empty($login3['num']) ? 0 : $login3['num'],
                    'recharge3' => empty($recharge3['num']) ? 0 : $recharge3['num'],
                    'diamond3' => empty($diamond3['num']) ? 0 : abs($diamond3['num']),
                    'login7' => empty($login7['num']) ? 0 : $login7['num'],
                    'recharge7' => empty($recharge7['num']) ? 0 : $recharge7['num'],
                    'diamond7' => empty($diamond7['num']) ? 0 : abs($diamond7['num']),
                ];
                $updatas[] = $updata;
            }else{
                $insert = [
                    'uuid' => $uuid,
                    'userId' => $userId,
                    'registerDay' => date("Y-m-d H:i:s", $register),
                    'rechargeAll' => empty($rechargeAll['num']) ? 0 : $rechargeAll['num'],
                    'diamondAll' => empty($diamondAll['num']) ? 0 : abs($diamondAll['num']),
                    'recharge1' => empty($recharge1['num']) ? 0 : $recharge1['num'],
                    'diamond1' => empty($diamond1['num']) ? 0 : abs($diamond1['num']),
                    'login3' => empty($login3['num']) ? 0 : $login3['num'],
                    'recharge3' => empty($recharge3['num']) ? 0 : $recharge3['num'],
                    'diamond3' => empty($diamond3['num']) ? 0 : abs($diamond3['num']),
                    'login7' => empty($login7['num']) ? 0 : $login7['num'],
                    'recharge7' => empty($recharge7['num']) ? 0 : $recharge7['num'],
                    'diamond7' => empty($diamond7['num']) ? 0 : abs($diamond7['num']),
                ];
                $inserts[] = $insert;
            }
        }
        //调用model
        if (!empty($inserts))
        {
            $res = $this->kovModel->checkAndInsert($inserts);
        }else{
            $res = $this->kovModel->UpdateData($updatas);
        }
        return $res;
    }
    //返回数组
    public function objToArray($document)
    {
        return json_decode(json_encode($document), true);
    }
}

